package com.house.service.impl;

import com.common.constant.enums.CommonStatus;
import com.common.constant.strings.ErrorMsg;
import com.common.constant.strings.SuccessMsg;
import com.common.entity.TbCity;
import com.common.entity.house.HouseResource;
import com.common.entity.house.HouseResourceImage;
import com.common.response.CommonResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.house.mapper.HouseResourceImageMapper;
import com.house.mapper.HouseResourceMapper;
import com.house.service.HouseResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HouseResourceImpl implements HouseResourceService {

    @Autowired
    private HouseResourceMapper houseResourceMapper;

    @Autowired
    private HouseResourceImageMapper houseResourceImageMapper;

    @Override
    public List<TbCity> getCityList(Integer pid) {
        return houseResourceMapper.getCityList(pid);
    }

    @Override
    public PageInfo<HouseResource> getPage(Integer page, Integer size, HouseResource houseResource) {
        PageHelper.startPage(page,size);
        List<HouseResource> list = houseResourceMapper.getPage(houseResource);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<HouseResource> getRentPage(Integer page, Integer size, HouseResource houseResource) {
        PageHelper.startPage(page,size);
        List<HouseResource> list = houseResourceMapper.getRentPage(houseResource);
        return new PageInfo<>(list);
    }

    @Override
    public CommonResponse updateStatusById(Long id, Integer status) {
        try {
            houseResourceMapper.updateStatusById(id,status);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(), ErrorMsg.ERROR_OPERATION_404);
        }
    }

    @Override
    public CommonResponse delete(Long id) {
        try {
            houseResourceMapper.delete(id);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(), ErrorMsg.ERROR_OPERATION_404);
        }
    }

    @Override
    public CommonResponse insert(HouseResource houseResource,String[] images) {
        try {
            houseResource.setCreateTime(new Date());
            houseResource.setMakeBargain(1);
            houseResource.setIsDelete(0);
            houseResourceMapper.insert(houseResource);
            for (String image : images) {
                HouseResourceImage houseResourceImage = new HouseResourceImage();
                houseResourceImage.setHid(houseResource.getId());
                houseResourceImage.setImage(image);
                houseResourceImageMapper.insert(houseResourceImage);
            }
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(), ErrorMsg.ERROR_OPERATION_404);
        }
    }

    @Override
    public CommonResponse bingBroker(Long id, Long brokerId) {
        try {
            houseResourceMapper.bingBroker(id,brokerId);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(), ErrorMsg.ERROR_OPERATION_404);
        }
    }

    @Override
    public CommonResponse insertRent(String[] images, HouseResource houseResource) {
        try {
            houseResource.setCreateTime(new Date());
            houseResource.setMakeBargain(1);
            houseResource.setIsDelete(0);
            houseResourceMapper.insert(houseResource);
            for (String image : images) {
                HouseResourceImage houseResourceImage = new HouseResourceImage();
                houseResourceImage.setHid(houseResource.getId());
                houseResourceImage.setImage(image);
                houseResourceImageMapper.insert(houseResourceImage);
            }
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(), ErrorMsg.ERROR_OPERATION_404);
        }
    }
}
